package com.srv.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.srv.control.HashControl;
import com.srv.dao.DAOUsuario;
import com.srv.exceptions.UsuarioInvalidoException;
import com.srv.pojo.Endereco;
import com.srv.pojo.Usuario;
//import com.srv.control.HashControl;

@WebServlet("/CadastraUsuarioServlet")
public class CadastraUsuarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CadastraUsuarioServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			Usuario usuario = new Usuario();
			String aux = request.getParameter("email");
			if (aux.contains("--")){
				throw new UsuarioInvalidoException("O email informado não pode conter os caracteres \"--\"");
			}
			usuario.setEmail(aux);
			usuario.setSenha(HashControl.md5(request.getParameter("senha")));
			aux = request.getParameter("nome") + " " + request.getParameter("sobrenome");
			if (aux.contains("--")){
				throw new UsuarioInvalidoException("O nome informado não pode conter os caracteres \"--\"");
			}
			usuario.setNome(aux);
			usuario.setCpf(request.getParameter("cpf").replaceAll("\\p{Punct}*(-)*", ""));
			usuario.setRg(request.getParameter("rg").replaceAll("\\p{Punct}*(-)*", ""));
			usuario.setFone1(request.getParameter("telefone").replaceAll("\\D*(-)*", ""));
			usuario.setFone2(request.getParameter("celular").replaceAll("\\D*(-)*", ""));
			usuario.setNascimento(new SimpleDateFormat("dd/MM/yyyy").parse(request.getParameter("dataNascimento")));
			
			// TODO : ajustar para permitir a escolha do endereco-------------------------
			Endereco endereco = new Endereco();
			endereco.setEndcode((long)600236);
			usuario.setEndereco(endereco);
			//---------------------------------------------------------------------------
			
			usuario.setComplemento(request.getParameter("complemento").replaceAll("--", ""));
			usuario.setEnderecoNumero(Integer.parseInt(request.getParameter("enderecoNumero")));
			DAOUsuario daoUsuario = new DAOUsuario();
			daoUsuario.insert(usuario);
			request.setAttribute("msg", "Usuario Cadastrado com sucesso!");
			RequestDispatcher view = request.getRequestDispatcher("index.jsp");
			view.forward(request, response);
		} catch (Exception e) {
			request.setAttribute("msg", "Ocorreu um erro ao tentar cadastrar o usuário!\r" + e.getMessage());
			RequestDispatcher view = request.getRequestDispatcher("index.jsp");
			view.forward(request, response);
		}
	}
}
